var e = require("../@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var r = e(require("../@babel/runtime/regenerator")), t = e(require("../@babel/runtime/helpers/asyncToGenerator")), n = e(require("../utils/wx-request/lib/index")), a = (e(require("./wxlogin")), 
require("../utils/util")), u = require("./baseURL"), s = getApp();

function i() {
    return o.apply(this, arguments);
}

function o() {
    return (o = (0, t.default)(r.default.mark(function e() {
        var t, a, u = arguments;
        return r.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                return t = u.length > 0 && void 0 !== u[0] ? u[0] : {}, (a = new n.default(t)).interceptors.use({
                    request: function(e) {
                        return !1 !== t.loading && wx.showLoading({
                            title: "加载中...",
                            mask: !0
                        }), e;
                    },
                    requestError: function(e) {
                        return wx.hideLoading(), Promise.reject(e);
                    },
                    response: function(e) {
                        return !1 !== t.loading && wx.hideLoading(), e;
                    },
                    responseError: function(e) {
                        return wx.hideToast(), wx.showToast({
                            title: "请求失败",
                            icon: "none",
                            mask: !0,
                            image: "/images/error.png",
                            duration: 3e3
                        }), Promise.reject(e);
                    }
                }), e.next = 5, a.request();

              case 5:
                return e.abrupt("return", e.sent);

              case 6:
              case "end":
                return e.stop();
            }
        }, e);
    }))).apply(this, arguments);
}

var c = function() {
    var e = (0, t.default)(r.default.mark(function e(t, n) {
        var o, c, l, p;
        return r.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                c = 0;

              case 1:
                if (!(c < 3)) {
                    e.next = 29;
                    break;
                }
                if (e.prev = 2, (l = (0, a.getAuth)()) && !l.value && (0, a.setToken)(l, (0, a.getUUID)()), 
                !s.globalData.isLock) {
                    e.next = 8;
                    break;
                }
                return e.next = 8, new Promise(function(e, r) {
                    s.tokenObservers.push(e);
                });

              case 8:
                return l = (0, a.getAuth)(), l = (0, a.getAuth)(), e.next = 12, i(Object.assign({}, {
                    baseURL: u.baseURL,
                    url: t,
                    data: n.body,
                    header: {
                        "x-api-key": "".concat(l.value)
                    }
                }, n));

              case 12:
                if (401 !== (p = e.sent).data.code) {
                    e.next = 18;
                    break;
                }
                return wx.navigateTo({
                    url: "/pages/login/index"
                }), e.abrupt("break", 29);

              case 18:
                return o = p, e.abrupt("break", 29);

              case 20:
                e.next = 26;
                break;

              case 22:
                return e.prev = 22, e.t0 = e.catch(2), console.log(e.t0), e.abrupt("break", 29);

              case 26:
                c++, e.next = 1;
                break;

              case 29:
                if (!o) {
                    e.next = 33;
                    break;
                }
                return e.abrupt("return", o);

              case 33:
                return e.abrupt("return", {
                    data: {
                        msg: "请求失败",
                        code: 0
                    }
                });

              case 34:
              case "end":
                return e.stop();
            }
        }, e, null, [ [ 2, 22 ] ]);
    }));
    return function(r, t) {
        return e.apply(this, arguments);
    };
}();

exports.default = c;